﻿#include "stdafx.h"
#include "Models_player.h"
#include "Animate.h"

Models_player *Models_player::s_inst;

Models_player::Models_player()
{
    m_boundingBoxMax = glm::vec3(0.0f);
    m_boundingBoxMin = glm::vec3(0.0f);
}


Models_player::~Models_player()
{
}

void Models_player::BoundBox()
{
	m_boundingBoxMin = glm::vec3(-50.0f, -50.0f, 0.f);
	m_boundingBoxMax = glm::vec3(50.0f, 50.0f, 190.0f);
	/*Animate anim(&m_mdl_player.m_animationData);
	glm::mat4 *mats = nullptr;
	int count = anim.GetBoneTransformMatrixs(&mats);
	glm::vec4 v(0, 0, 0, 1);
	bool fFirstVertex = true;
	for (int i = 0; i < count; i++)
	{
		glm::vec4 v4 = mats[i] * v;
		if (fFirstVertex)
		{
			fFirstVertex = false;
			for (int i = 0; i < 3; i++)
			{
				m_boundingBoxMax[i] = m_boundingBoxMin[i] = v4[i];
			}
		}
		else
		{
			float f1;
			for (int j = 0; j<3; j++)
			{
				f1 = v4[j];
				if (m_boundingBoxMin[j]>f1) m_boundingBoxMin[j] = f1;
				if (m_boundingBoxMax[j]<f1) m_boundingBoxMax[j] = f1;
			}
		}
	}*/
}

Models_player *Models_player::GetInstance()
{
	if (!s_inst)
	{
		const char *partName[] = { "HelmClass", "ArmorClass", "GloveClass", "PantClass", "BootClass" };
		char path[MAX_PATH];
		s_inst = new Models_player;
		bool fok = s_inst->m_mdl_player.Load("./Data/Player/Player.bmd", 0);
		assert(fok);
		s_inst->m_texMgr.SetLoadDir("./Data/Player/");
		for (int i = 0; i < 5; i++)
		{
			for (int j = 0; j < MuType::Class_Count; j++)
			{
				sprintf(path, "./Data/Player/%s%02d.bmd", partName[i], j + 1);
				fok = s_inst->m_mdl_parts[j][i].Load(path, &s_inst->m_texMgr);
				assert(fok);
			}
		}
		s_inst->BoundBox();
	}
	return s_inst;
}

void Models_player::GetBody(MuType::CharacterClass muClass, std::vector<Model*> &out)
{
	for (auto &m : m_mdl_parts[muClass]) out.push_back(&m);
}

Model *Models_player::GetBody(MuType::CharacterClass muClass, int *outCount)
{
	*outCount = 5;
	return m_mdl_parts[muClass];
}

AnimationData *Models_player::GetPlayerAnimation()
{
	return &m_mdl_player.m_animationData;
}